package cmd

import (
	"github.com/spf13/cobra"
	global2 "longmen/server/config/global"
	"longmen/server/internal/rpc/tencentim/api"
	"longmen/server/internal/services/consumers/config"
	"longmen/server/internal/services/consumers/consumer/tx_chat"
	_const "longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/lib/rabbitmq"
	"longmen/server/pkg/grpc_client"
)

var txChatAnonymousCli = &cobra.Command{
	Use:   _const.PusherAnonymousLogin,
	Short: "腾讯聊天室广播队列 ",
	Long:  "运行腾讯聊天室广播队列, 不可分布式运行，因为广播接口限制200次/秒",
	Run: func(cmd *cobra.Command, args []string) {
		mq := rabbitmq.New(&config.RabbitConf, global2.Log)
		exchange, queue, options, consumerOptions := rabbitmq.GetRabbitConsumerConfig(global2.Config, global2.AppName)
		// 开启consumer
		consumer, err := mq.NewConsumer(exchange, queue, options, consumerOptions)
		if err != nil {
			global2.Log.Fatalf("new consumer[%s]: err : %v", global2.AppName, err)
		}
		chat := tx_chat.NewAnonymousLogin(global2.Log)

		err = consumer.Consume(chat.Process)
		if err != nil {
			global2.Log.Fatalf("run consumer[%s]: err : %v", global2.AppName, err)
		}
		gracefulShutdown()
	},
	PreRun: func(cmd *cobra.Command, args []string) {
		global2.AppName = _const.PusherAnonymousLogin
		global2.Initialize(config.ConfigPath)
		config.InitConsumerConfig()
		api.InitAPI()
		grpc_client.InitGrpc()
		err := global2.InitElasticSearch()
		if err != nil {
			global2.Log.Fatalf("init es fail: %v", err)
		}

	},
}
